package com.zz.framework.core.ext

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Parcelable
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.zz.framework.core.utils.LifecycleHandler
import com.zz.framework.core.widget.XAlertDialog
import java.io.Serializable


inline fun <reified T : Activity> Activity.startActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair<String, Any>? = null,
    values: Collection<Pair<String, Any>?>? = null
) {

    val list = ArrayList<Pair<String, Any>?>()
    value?.let { list.add(it) }
    values?.let { list.addAll(it) }
    startActivity(getIntent<T>(flags, extra, list))
}

inline fun <reified T : Activity> Fragment.startActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair<String, Any>? = null,
    values: Collection<Pair<String, Any>?>? = null
) =
    activity?.let {
        val list = ArrayList<Pair<String, Any>?>()
        value?.let { v -> list.add(v) }
        values?.let { v -> list.addAll(v) }
        startActivity(it.getIntent<T>(flags, extra, list))
    }

inline fun <reified T : Activity> Context.startActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair<String, Any>? = null,
    values: Collection<Pair<String, Any>?>? = null
) {
    val list = ArrayList<Pair<String, Any>?>()
    value?.let { v -> list.add(v) }
    values?.let { v -> list.addAll(v) }
    startActivity(getIntent<T>(flags, extra, list))
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    requestCode: Int,
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair<String, Any>? = null,
    values: Collection<Pair<String, Any>?>? = null
) {
    val list = ArrayList<Pair<String, Any>?>()
    value?.let { list.add(it) }
    values?.let { list.addAll(it) }
    startActivityForResult(getIntent<T>(flags, extra, list), requestCode)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    requestCode: Int,
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair<String, Any>? = null,
    values: Collection<Pair<String, Any>?>? = null
) =
    activity?.let {
        val list = ArrayList<Pair<String, Any>?>()
        value?.let { list.add(it) }
        values?.let { list.addAll(it) }
        startActivityForResult(activity?.getIntent<T>(flags, extra, list), requestCode)
    }

inline fun <reified T : Context> Context.getIntent(
    flags: Int? = null,
    extra: Bundle? = null,
    pairs: List<Pair<String, Any>?>? = null
): Intent =
    Intent(this, T::class.java).apply {
        flags?.let { setFlags(flags) }
        extra?.let { putExtras(extra) }
        pairs?.let {
            for (pair in pairs)
                pair?.let {
                    val name = pair.first
                    when (val value = pair.second) {
                        is Int -> putExtra(name, value)
                        is Byte -> putExtra(name, value)
                        is Char -> putExtra(name, value)
                        is Short -> putExtra(name, value)
                        is Boolean -> putExtra(name, value)
                        is Long -> putExtra(name, value)
                        is Float -> putExtra(name, value)
                        is Double -> putExtra(name, value)
                        is String -> putExtra(name, value)
                        is CharSequence -> putExtra(name, value)
                        is Parcelable -> putExtra(name, value)
                        is Array<*> -> putExtra(name, value)
                        is ArrayList<*> -> putExtra(name, value)
                        is Serializable -> putExtra(name, value)
                        is BooleanArray -> putExtra(name, value)
                        is ByteArray -> putExtra(name, value)
                        is ShortArray -> putExtra(name, value)
                        is CharArray -> putExtra(name, value)
                        is IntArray -> putExtra(name, value)
                        is LongArray -> putExtra(name, value)
                        is FloatArray -> putExtra(name, value)
                        is DoubleArray -> putExtra(name, value)
                        is Bundle -> putExtra(name, value)
                        is Intent -> putExtra(name, value)
                        else -> {
                        }
                    }
                }
        }
    }


fun FragmentActivity.finishDelay(delay: Long = 1) {
    LifecycleHandler(this).postDelayed({ finish() }, delay)
}

//post, postDelay
fun FragmentActivity.post(action: ()->Unit){
    LifecycleHandler(this).post { action() }
}

fun FragmentActivity.postDelay(delay:Long = 200, action: ()->Unit){
    LifecycleHandler(this).postDelayed({ action() }, delay)
}

//view model
fun <T : ViewModel> FragmentActivity.getViewModel(clazz: Class<T>) = ViewModelProviders.of(this).get(clazz)



fun Activity.createPositiveDialog(
    @StringRes title: Int,
    @StringRes message: Int,
    @StringRes positiveText: Int = android.R.string.ok,
    positiveListener: DialogInterface.OnClickListener,
    isCanceledOnTouchOutside: Boolean,
    isCancelable: Boolean
): Dialog {
    val builder = XAlertDialog.Builder(this)
    builder.setTitle(title)
    builder.setMessage(message)
    builder.setPositiveButton(positiveText, positiveListener)
    val dialog = builder.create()
    dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
    dialog.setCancelable(isCancelable)
    dialog.setOnShowListener {
        val button = dialog?.getButton(DialogInterface.BUTTON_POSITIVE);
        button?.setTextColor(Color.parseColor("#FF5C5E"));
    }
    return dialog
}
fun Activity.createPositiveDialog(
    title: String,
    message: String,
    @StringRes positiveText: Int = android.R.string.ok,
    positiveListener: DialogInterface.OnClickListener,
    isCanceledOnTouchOutside: Boolean,
    isCancelable: Boolean
): Dialog {
    val builder = XAlertDialog.Builder(this)
    builder.setTitle(title)
    builder.setMessage(message)
    builder.setPositiveButton(positiveText, positiveListener)
    val dialog = builder.create()
    dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
    dialog.setCancelable(isCancelable)
    dialog.setOnShowListener {
        val button = dialog?.getButton(DialogInterface.BUTTON_POSITIVE);
        button?.setTextColor(Color.parseColor("#FF5C5E"));
    }
    return dialog
}

@SuppressLint("SuspiciousIndentation")
fun Activity.createDialog(
    @StringRes title: Int = 0,
    @StringRes message: Int,
    @StringRes negativeText: Int = 0,
    negativeListener: DialogInterface.OnClickListener,
    @StringRes positiveText: Int = android.R.string.ok,
    positiveListener: DialogInterface.OnClickListener,
    isCanceledOnTouchOutside: Boolean = true,
    isCancelable: Boolean = true
): Dialog {
    val builder = XAlertDialog.Builder(this)
    if (title != 0)
    builder.setTitle(title)
    builder.setMessage(message)
    builder.setPositiveButton(positiveText, positiveListener)
    if (negativeText!=0) {
        builder.setNegativeButton(negativeText, negativeListener)
    }
    val dialog = builder.create()
    dialog.setOnShowListener {
        val button = dialog?.getButton(DialogInterface.BUTTON_POSITIVE);
        button?.setTextColor(Color.parseColor("#FF5C5E"));
        val negativeButton = dialog?.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton?.setTextColor(Color.parseColor("#ff0000"));
    }
    dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
    dialog.setCancelable(isCancelable)
    return dialog
}
fun Activity.createErrorDialog(
    message: String?,
    @StringRes positiveText: Int = android.R.string.ok,
    positiveListener: DialogInterface.OnClickListener = DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() },
    isCanceledOnTouchOutside: Boolean = true,
    isCancelable: Boolean = true
): Dialog {
    val builder = XAlertDialog.Builder(this)
    builder.setMessage(message)
    builder.setPositiveButton(positiveText, positiveListener)
    val dialog = builder.create()
    dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
    dialog.setCancelable(isCancelable)
    dialog.setOnShowListener {
        val button = dialog?.getButton(DialogInterface.BUTTON_POSITIVE);
        button?.setTextColor(Color.parseColor("#ff0000"));
    }
    return dialog
}